package com.zhou.bpm.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.bpm.domain.dto.WorkspaceRequest;
import com.zhou.bpm.domain.vo.HistoryVo;
import com.zhou.bpm.domain.vo.WorkspaceVo;
import com.zhou.bpm.enmu.InstanceStatusEnum;
import com.zhou.bpm.entity.BpmInstance;
import com.zhou.bpm.entity.BpmNode;
import com.zhou.bpm.entity.BpmTask;
import com.zhou.bpm.mapper.BpmTaskApproverMapper;
import com.zhou.bpm.mapper.BpmTaskMapper;
import com.zhou.bpm.service.IBpmInstanceService;
import com.zhou.bpm.service.IBpmNodeService;
import com.zhou.bpm.service.IWorkspaceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

import static com.zhou.common.Constants.FINISH_NO;

/**
 * @Author: zhoujinchuan
 * @Description: TODO（请添加描述）
 * @CreateTime: 2023/10/26 10:38
 */
@Service
public class WorkspaceService implements IWorkspaceService {

    @Autowired
    private BpmTaskMapper bpmTaskMapper;

    @Autowired
    private IBpmNodeService nodeService;

    @Autowired
    private IBpmInstanceService instanceService;

    @Autowired
    private BpmTaskApproverMapper taskApproverMapper;


    @Override
    public List<HistoryVo> approvalProcess(Integer instanceId) {
        BpmInstance bpmInstance = instanceService.getById(instanceId);
        Assert.notNull(bpmInstance, "流程信息不存在");
        String processCode = bpmInstance.getProcessCode();
        //获取审批中的流程
        List<HistoryVo> list = bpmTaskMapper.approvalProcess(processCode, instanceId);
        if (FINISH_NO.equals(bpmInstance.getCurrentNodeCode())) {
            HistoryVo historyVo = new HistoryVo();
            historyVo.setNodeCode("-1");
            historyVo.setNodeName("结束");
            list.add(historyVo);
        } else {
            List<BpmNode> behindNodeList = nodeService.getBehindNode(processCode, bpmInstance.getCurrentNodeCode());
            if (!CollectionUtils.isEmpty(behindNodeList)) {
                list.addAll(behindNodeList.stream().map(e -> {
                    HistoryVo historyVo = new HistoryVo();
                    historyVo.setNodeCode(e.getNodeCode());
                    historyVo.setNodeName(e.getNodeName());
                    return historyVo;
                }).collect(Collectors.toList()));
            }
        }
        return list;
    }

    @Override
    public Page<WorkspaceVo> pageQuery(Integer pageNum, Integer pageSize, Integer accountCode, WorkspaceRequest workspaceRequest, String type) {

        Page<BpmTask> page = new Page<>(pageNum, pageSize);
        List<WorkspaceVo> list = null;
        if ("1".equals(type)) {
            list = bpmTaskMapper.pageQueryApply(page, accountCode, workspaceRequest);
        } else if ("2".equals(type)) {
            list = bpmTaskMapper.pageQueryTodoList(page, accountCode, workspaceRequest);
        } else {
            list = bpmTaskMapper.pageQueryApproved(page, accountCode, workspaceRequest);
        }
        Page<WorkspaceVo> resultPage = new Page<>();
        BeanUtils.copyProperties(page, resultPage, "records");
        for (WorkspaceVo workspaceVo : list) {
            workspaceVo.setStatusStr(InstanceStatusEnum.getLabelByCode(workspaceVo.getStatus() + ""));
            if (FINISH_NO.equals(workspaceVo.getCurrentNodeName())) {
                workspaceVo.setCurrentNodeName("无");
            }
        }
        resultPage.setRecords(list);
        return resultPage;
    }

}
